package org.jiamandan.com.utils;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.dto.OrderDto;
import org.jiamandan.com.dto.ZDBasicDto;
import org.jiamandan.com.entity.RechargePile;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Jmd
 * @see:
 * @创建日期：2022-11-29 15:34
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2022-11-29 15:34	         Jmd        	创建
 * @end
 */
public class ZDObjectUtil {

    // 开始字符(长度1->2，数据长度2->4，设备编号16->32，命令代码2->4，数据域N->0，校验和 1->2，结束字符 1->2
    private static int[] bodyLength = {2, 4, 32, 4, 0, 2, 2};
    //开始字符(长度3->6，数据长度2->4，物理ID 4->8，消息ID 2->4，命令1->2, 数据域N->0，校验和 2->4
    private static int[] ydbodyLength = {6, 4, 8, 4, 2, 0, 4};

    /**
     * 转换为16进制原报文
     *
     * @param payload
     * @return
     */
    public static ZDBasicDto parse(String payload) {
        ZDBasicDto dto = new ZDBasicDto();
        String prefix = StringUtils.substring(payload, 0, 2);
        if ("68".equals(prefix)) {
            dto.setStartPrefix(StringUtils.substring(payload, 0, bodyLength[0]));
            String dataLength = StringUtils.substring(payload, 2, 2 + bodyLength[1]);
            dto.setDataLength(dataLength);
            dto.setDeviceNo(StringUtils.substring(payload, 6, 6 + bodyLength[2]));
            dto.setCmd(StringUtils.substring(payload, 38, 38 + bodyLength[3]));
            int total = payload.length(),
                    sureLength = bodyLength[0] + bodyLength[1] + bodyLength[2] + bodyLength[3] + bodyLength[5] + bodyLength[6];
            dto.setData(StringUtils.substring(payload, 42, total - sureLength + 42));
            dto.setValidate(StringUtils.substring(payload, total - 4, total - 2));
            dto.setEndPrefix(StringUtils.substring(payload, total - 2, total));
            return dto;
        }
        if ("44".equals(prefix)) {
            dto.setStartPrefix(StringUtils.substring(payload, 0, ydbodyLength[0]));
            String dataLength = StringUtils.substring(payload, 6, 6 + ydbodyLength[1]);
            dto.setDataLength(dataLength);
            dto.setDeviceNo(StringUtils.substring(payload, 10, 10 + ydbodyLength[2]));
            dto.setEndPrefix(StringUtils.substring(payload, 18, 18 + ydbodyLength[3]));
            dto.setCmd(StringUtils.substring(payload, 22, 22 + ydbodyLength[4]));
            int total = payload.length(),
                    sureLength = ydbodyLength[0] + ydbodyLength[1] + ydbodyLength[2] + ydbodyLength[3] + ydbodyLength[4] + ydbodyLength[6];
            dto.setData(StringUtils.substring(payload, 24, total - sureLength + 24));
            dto.setValidate(StringUtils.substring(payload, total - 4, total - 2));
            return dto;
        }
        if ("47".equals(prefix)) {
            dto.setStartPrefix(StringUtils.substring(payload, 0, ydbodyLength[0]));
            String dataLength = StringUtils.substring(payload, 6, 6 + ydbodyLength[1]);
            dto.setDataLength(dataLength);
            dto.setDeviceNo(StringUtils.substring(payload, 10, 10 + ydbodyLength[2]));
            dto.setEndPrefix(StringUtils.substring(payload, 18, 18 + ydbodyLength[3]));
            dto.setCmd(StringUtils.substring(payload, 22, 22 + ydbodyLength[4]));
            int total = payload.length(),
                    sureLength = ydbodyLength[0] + ydbodyLength[1] + ydbodyLength[2] + ydbodyLength[3] + ydbodyLength[4] + ydbodyLength[6];
            dto.setData(StringUtils.substring(payload, 24, total - sureLength + 24));
            dto.setValidate(StringUtils.substring(payload, total - 4, total - 2));
            return dto;
        }
        return null;

    }

    public static ZDBasicDto parse2(String payload) {
        ZDBasicDto dto = new ZDBasicDto();
        dto.setStartPrefix(StringUtils.substring(payload, 0, bodyLength[0]));
        String dataLength = StringUtils.substring(payload, 2, bodyLength[1]);
        dto.setDataLength(dataLength);
        dto.setDeviceNo(StringUtils.substring(payload, 6, bodyLength[2]));
        dto.setCmd(StringUtils.substring(payload, 38, bodyLength[3]));
        int total = payload.length(),
                sureLength = bodyLength[0] + bodyLength[1] + bodyLength[2] + bodyLength[3] + bodyLength[5] + bodyLength[6];
        dto.setData(StringUtils.substring(payload, 42, total - sureLength));
        dto.setValidate(StringUtils.substring(payload, total - 4, total - 2));
        dto.setEndPrefix(StringUtils.substring(payload, total - 2, total));
        return dto;
    }

    /**
     * 转换为大端10进制报文
     *
     * @param payload
     * @return
     */
    public static ZDBasicDto parseObject(String payload) {
        ZDBasicDto dto = parse(payload);
        return dto;
    }


    /**
     * JMD-ZD拼接报文
     *
     * @param payload
     * @return
     */
    public static String splicingMessage(String payload, String deviceNo, String cmd) {
        String deviceNoHex = StringUtils.rightPad(CommonUtil.toHexString(deviceNo), 32, '0');
        String cmdHex = CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(Integer.valueOf(cmd)), 4, "0"));
        StringBuilder content = new StringBuilder(deviceNoHex)
                .append(cmdHex)
                .append(payload);
        StringBuilder builder = new StringBuilder();

        builder.append("68")
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(content.length() / 2), 4, "0")))
                .append(content.toString())
                .append(CommonUtil.makeChecksum(content.toString()))
                .append("16");
        return builder.toString();
    }

    /**
     * JMD-ZD下发拼接报文
     *
     * @param payload
     * @return
     */
    public static String pushMessage(String payload, String deviceNo, String cmd) {
        StringBuilder content = new StringBuilder(StringUtils.rightPad(CommonUtil.toHexString(deviceNo), 32, "0"))
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(Integer.valueOf(cmd)), 4, "0")))
                .append(payload);
        StringBuilder builder = new StringBuilder();
        builder.append("68")
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(content.length() / 2), 4, "0")))
                .append(content.toString())
                .append(CommonUtil.makeChecksum(content.toString()))
                .append("16");
        return builder.toString();
    }

    /**
     * JMD-YD应答拼接报文
     *
     * @param payload  报文
     * @param dto  接收报文
     * @param cmd  命令
     * @return
     */
    public static String ydpushMessage(String payload, ZDBasicDto dto, String cmd) {
        //44 4E 59 0A 00 3B 37 AB 04 B9 00 01 00 D0 0
        //长度=物理ID+消息ID+命令+数据(n) +校验(2)
        //校验从包头到数据的内容
        StringBuilder content = new StringBuilder();
        //物理ID+消息ID+命令+数据(n)
        content.append(dto.getDeviceNo())
                .append(dto.getEndPrefix())
                .append(StringUtils.leftPad(cmd,2,"0"))
                .append(payload);
        StringBuilder builder = new StringBuilder();
        builder.append(dto.getStartPrefix())
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString((content.length() / 2)+2), 4, "0")))
                .append(content);
        String sum = CommonUtil.ydmakeChecksum(builder.toString());
        //添加校验和
        builder.append(sum);
        return builder.toString();
    }

    /**
     * JMD-YD下发拼接报文
     *
     * @param payload  报文
     * @param dto  接收报文
     * @param cmd  命令
     * @return
     */
    public static String ydsplicingMessage(String payload, RechargePile dto,String gunNo, String cmd) {
        //44 4E 59
        // 26 00
        // 3B 37 AB 04
        // 02 00
        // 82
        // 00   64 01 00 00    01   01   00 00 12 34 56 78 12 34 56 78 12 34 56 78 12 34 56 78 80 70 88 13
        // F8 08
        //长度=物理ID+消息ID+命令+数据(n) +校验(2)
        //校验从包头到数据的内容
        StringBuilder content = new StringBuilder();
        //物理ID+消息ID+命令+数据(n)
        content.append(CommonUtil.reverseHex(Integer.toHexString(Integer.valueOf(dto.getPileNo())))+StringUtils.leftPad(StrUtil.toString(dto.getCode()),2,"0"))
                .append(CommonUtil.reverseHex(StringUtils.leftPad(gunNo,4,"0")))
                .append(StringUtils.leftPad(cmd,2,"0"))
                .append(payload);
        StringBuilder builder = new StringBuilder();
        builder.append("444E59")
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString((content.length() / 2)+2), 4, "0")))
                .append(content);
        String sum = CommonUtil.ydmakeChecksum(builder.toString());
        //添加校验和
        builder.append(sum);
        return builder.toString();
    }


}
